package com.hxzy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.exception.ServiceStringException;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.dict.req.*;
import com.hxzy.controller.admin.dict.resp.DictTypeResp;
import com.hxzy.entity.SysDictData;
import com.hxzy.entity.SysDictType;
import com.hxzy.mapstruct.DictTypeMapStruct;
import com.hxzy.service.SysDictDataService;
import com.hxzy.service.SysDictTypeService;
import com.hxzy.mapper.SysDictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.GenericArrayType;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author admin
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2024-02-21 11:30:41
*/
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements SysDictTypeService{


    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 分页查询
     * @param req
     * @return
     */
    @Override
    public List<DictTypeResp> search(DictTypeReq req) {
        return this.baseMapper.search(req);
    }

    /**
     * 新增数据
     * @param req
     * @return
     */
    @Override
    public boolean addData(DictTypeAddReq req) {
        //验证字典类型是否存在
        LambdaQueryWrapper<SysDictType> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getDictType,req.getDictType());
        long count=this.count(queryWrapper);
        if(count>0){
            throw new ServiceException(AckCode.EXISTS_VALUE);
        }

        SysDictType entity = DictTypeMapStruct.INSTANCE.convert(req);
        entity.setCreateTime(new Date());
        entity.setCreateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());

        return this.save(entity);
    }

    @Override
    public boolean updateData(DictTypeEditReq req) {
        SysDictType db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }

        //如果禁用
        if(MarketConstants.ACCOUT_DISABLE.equals(req.getStatus())) {
            LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, db.getDictType());
            queryWrapper.eq(SysDictData::getStatus, MarketConstants.ACCOUT_ENABLE);
            long count = this.sysDictDataService.count(queryWrapper);
            //有关联的数据，不允许禁用
            if (count > 0) {
                throw new ServiceStringException(StrUtil.format("{}存在关联数据，不允许操作", db.getDictType()));
            }
        }

        //复制对象
        SysDictType convert = DictTypeMapStruct.INSTANCE.convert(req);
        convert.setUpdateTime(new Date());
        convert.setUpdateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());

        return this.updateById(convert);
    }

    /**
     * 更改账户状态
     * @param req
     * @return
     */
    @Override
    public boolean changeStatus(DictTypeStatusReq req) {
        SysDictType db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }
        //1、查询sys_dict_data中的类型个数>0 不允许禁用
        if(MarketConstants.ACCOUT_DISABLE.equals(req.getStatus())){
            LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictData::getDictType, db.getDictType());
            queryWrapper.eq(SysDictData::getStatus,MarketConstants.ACCOUT_ENABLE);

            long count=this.sysDictDataService.count(queryWrapper);
            //有关联的数据，不允许禁用
            if(count>0){
                throw new ServiceStringException(StrUtil.format("{}存在关联数据，不允许操作",db.getDictType()));
            }
        }

        db.setUpdateTime(new Date());
        db.setUpdateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        db.setStatus(req.getStatus());

        return this.updateById(db);
    }

    @Override
    public List<DictTypeResp> exportExcel(DictTypeExportIdsReq req) {
        return this.baseMapper.exportExcel(req.getIds());
    }
}




